package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.FoodDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDemoImpl;
import com.foreknow.elm.dao.impl.FoodDaoImpl;
import com.foreknow.elm.dao.impl.OrderdetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public int createOrders(String userId, Integer businessId, Integer daId, Double orderTotal) {
        Integer orderId = 0;
        List<Orderdetailet> list1 = new ArrayList<>();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        CartDao cartDao = new CartDemoImpl();
        Cart cart = new Cart();
        cart.setUserId(userId);
//        cart.setBusinessId(businessId);
        try {
            DBUtil.beginTransaction();
            List<Cart> list = cartDao.listCart(cart);
            orderId = ordersDao.createOrders(userId, businessId,daId, orderTotal);

            for (Cart c :list) {
                //获取到cart对象中的 foodId和数量
                Orderdetailet orderdetailet = new Orderdetailet();
                orderdetailet.setOrderId(orderId);
                orderdetailet.setFoodId(c.getFoodId());
                orderdetailet.setQuantity(c.getQuantity());
                list1.add(orderdetailet);
            }
            orderdetailetDao.saveOrderdetailet(list1);
            cartDao.removeCart(userId,businessId,cart.getFoodId());
            DBUtil.commitTransaction();
            try {

                DBUtil.rollbackTransaction();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = null;

        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
//        FoodDao foodDao = new FoodDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        List<Orderdetailet> orderdetailetList = new ArrayList<>();
        try {
            orderdetailetList = orderdetailetDao.listOrderdetailet(orderId);
            orders = ordersDao.getOrdersById(orderId);
            orders.setOrderdetailet(orderdetailetList);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }

        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {

        List<Orders> ordersList = null;
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        List<Orderdetailet> orderdetailetList = new ArrayList<>();
        try {

            ordersList = ordersDao.listOrdersByUserId(userId);
            for (Orders orders:ordersList) {
                Integer orderId = orders.getOrderId();
                List<Orderdetailet> orderdetailets = orderdetailetDao.listOrderdetailet(orderId);
                orders.setOrderdetailet(orderdetailets);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return ordersList;
    }
}
